/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package holidaysManager;

import app.Global;
import app.MyController;
import app.MyModel;
import app.MyView;
import entities.Holiday;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author yoshi
 */
public class HolidayManagerController implements MyController{

    MyView myView;
    
    @Override
    public HolidaysManagerDefault getMyView() {
        return (HolidaysManagerDefault) this.myView;
    }

    @Override
    public MyModel getMyModel() {
        return this.getMyView().getMyModel();
    }

    @Override
    public void setMyView(MyView myView) {
        this.myView = myView;
    }
    
    public void delete(Holiday holiday){
        Connection connection = Global.getConnection();
        PreparedStatement pst = null;
        try {
            pst = connection.prepareStatement("DELETE FROM holidays WHERE id = ?");
            pst.setLong(1, holiday.getId());
            pst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(HolidayManagerController.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                pst.close();
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(HolidayManagerController.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                this.getMyView().refresh();
            }
        }
    }
    
    public void edit(Holiday holiday, Date date, String name){
        Connection connection = Global.getConnection();
        PreparedStatement pst = null;
        try {
            pst = connection.prepareStatement("UPDATE holidays SET date = ?, name = ? WHERE id = ?");
            pst.setDate(1, date);
            pst.setString(2, name);
            pst.setLong(3, holiday.getId());
            pst.executeUpdate();
        } catch (SQLException ex) {
        } finally {
            try {
                pst.close();
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(HolidayManagerController.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                this.getMyView().refresh();
            }
        }
    }
    
    public void add(Date date, String name){
        Connection connection = Global.getConnection();
        PreparedStatement pst = null;
        try {
            pst = connection.prepareStatement("INSERT INTO holidays(date, name) VALUES(?, ?)");
            pst.setDate(1, date);
            pst.setString(2, name);
            pst.executeUpdate();
        } catch (SQLException ex) {
        } finally {
            try {
                pst.close();
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(HolidayManagerController.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                this.getMyView().refresh();
            }
        }
    }
}
